package com.jshongli.myapplication.common.utils;

import android.text.TextUtils;
import android.util.Log;


import com.jshongli.myapplication.MyApplication;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author reuel
 */
public class DateUtils {


    //region String -> String 时间 格式转换
    public static String formatString(String str,String format,String newformat) {

        Date date = new Date();
        DateFormat format1 = new SimpleDateFormat(format);
        DateFormat format2 = new SimpleDateFormat(newformat);
        try {
            date = format1.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        str = format2.format(date);

        return str;
    }
    //endregion

    //region 时间差计算  开始-结束tm / hour
    public static double hourDifference(String tm1,String tm2,String format) {
//        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
//        String date = formatter.format(currentTime);
        double hours = 0;

        try {
            Date d1 = formatter.parse(tm2);
            Date d2 = formatter.parse(tm1);
            long diff = d1.getTime() - d2.getTime();// 这样得到的差值是微秒级别
            hours = (double) diff / (1000* 60 * 60 * 24);
        } catch (Exception e) {
        }
        return hours;
    }
    //endregion

    //region 时间差计算  开始-结束tm / 秒
    public static int hourDifference_s(String tm1,String tm2,String format) {
//        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
//        String date = formatter.format(currentTime);
        double hours = 0;

        try {
            Date d1 = formatter.parse(tm2);
            Date d2 = formatter.parse(tm1);
            long diff = d1.getTime() - d2.getTime();// 这样得到的差值是微秒级别
            hours = (double) diff / (1000);
        } catch (Exception e) {
        }
        return (int)hours;
    }
    //endregion

    //region 时间差计算  开始-结束tm / 分钟
    public static double minuteDifference(String tm1,String tm2,String format) {
//        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
//        String date = formatter.format(currentTime);
        double hours = 0;

        try {
            Date d1 = formatter.parse(tm2);
            Date d2 = formatter.parse(tm1);
            long diff = d1.getTime() - d2.getTime();// 这样得到的差值是微秒级别
            hours = (double) diff / (1000 * 60 );
        } catch (Exception e) {
        }
        return hours;
    }
    //endregion

    //region 获取当前时间
    public static String getNowTime(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
        String strDate = formatter.format(curDate);
        return strDate;
    }
    //endregion


    //region  date --> String

    //dateToMonth  yyyy-MM
    //dateToDay -- yyyy-MM-dd
    //dateToYear -- yyyy
    //dateToMonthCN -- yyyy年MM月
    //dateTomin  yyyy-MM-dd HH:mm:ss
    //dateTomin2  yyyy-MM-dd HH:mm:00
    public static String dateToString(Date date,String format) {
        return new SimpleDateFormat(format).format(date);
    }
    //endregion

    public static String stringtoCalenderTOP(String strTime) {
        String dateStr = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

            Date date = sdf.parse(strTime);

            Calendar calendar = Calendar.getInstance();

            calendar.setTime(date);

            calendar.set(Calendar.DATE, 1);

            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");

            dateStr = sdf2.format(calendar.getTime());
        } catch (ParseException x) {
            x.printStackTrace();
        }

        return dateStr;

    }

    public static String stringtoCalenderEND(String strTime) {
        String dateStr = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

            Date date = sdf.parse(strTime);

            Calendar calendar = Calendar.getInstance();

            calendar.setTime(date);

            int day = getMonthDays(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1);

            calendar.set(Calendar.DATE, day);

            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");

            dateStr = sdf2.format(calendar.getTime());
        } catch (ParseException x) {
            x.printStackTrace();
        }

        return dateStr;

    }



    //region long --> String
    // long类型转换为String类型
    // currentTime要转换的long类型的时间
    // formatType要转换的string类型的时间格式
    public static String longToString(long currentTime, String formatType)
            throws ParseException {
        Date date = longToDate(currentTime, formatType); // long类型转成Date类型
        String strTime = dateToString(date, formatType); // date类型转成String
        return strTime;
    }
    //endregion

    // string类型转换为date类型
    // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒，
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    // long转换为Date类型
    // currentTime要转换的long类型的时间
    // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    public static Date longToDate(long currentTime, String formatType)
            throws ParseException {
        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }

    // string类型转换为long类型
    // strTime要转换的String类型的时间
    // formatType时间格式
    // strTime的时间格式和formatType的时间格式必须相同
    public static long stringToLong(String strTime, String formatType)
            throws ParseException {
        Date date = stringToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    // date类型转换为long类型
    // date要转换的date类型的时间
    public static long dateToLong(Date date) {
        return date.getTime();
    }

    // 获取本月
    public static String curMonthCN() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月");
        Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
        String strDate = formatter.format(curDate);
        return strDate;
    }

    public static String curMonth() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
        String strDate = formatter.format(curDate);
        return strDate;
    }


    public static String dateWithWeekday(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM月dd日");
        Date date1 = null;
        try {
            date1 = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (date1 != null) {
            int weekday = date1.getDay();
            String after = "-";
            try {
                if (IsToday(date)) {
                    after = "今天 " + sdf1.format(date1) + " " + praseWeekday(weekday);
                } else if (IsYesterday(date)) {
                    after = "昨天 " + sdf1.format(date1) + " " + praseWeekday(weekday);
                } else {
                    after = sdf1.format(date1) + " " + praseWeekday(weekday);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return after;
        } else {
            return "-";
        }

    }

    public static String date1(String date) {
        if (!TextUtils.isEmpty(date)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = null;
            try {
                date1 = sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (date1 != null) {
                String after = sdf.format(date1);
                return after;
            } else {
                return "-";
            }
        } else {
            return "-";
        }
    }

    public static String date(String date) {
        if (!TextUtils.isEmpty(date)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf1 = new SimpleDateFormat("MM月dd日");
            Date date1 = null;
            try {
                date1 = sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (date1 != null) {
                String after = sdf1.format(date1);
                try {
                    if (IsDayAfterTomorrow(date)) {
                        after = "后天";
                    } else if (IsTomorrow(date)) {
                        after = "明天";
                    } else if (IsToday(date)) {
                        after = "今天";
                    } else if (IsYesterday(date)) {
                        after = "昨天";
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return after;
            } else {
                return "-";
            }
        } else {
            return "-";
        }
    }

    /**
     * 判断是否为后天(效率比较高)
     */
    public static boolean IsDayAfterTomorrow(String day) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = sdf.parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 2) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否为明天(效率比较高)
     */
    public static boolean IsTomorrow(String day) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = sdf.parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否为今天(效率比较高)
     */
    public static boolean IsToday(String day) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = sdf.parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否为昨天(效率比较高)
     */
    public static boolean IsYesterday(String day) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = sdf.parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == -1) {
                return true;
            }
        }
        return false;
    }

    private static String praseWeekday(int weekday) {

        if (weekday == 0) {
            return "星期日";
        } else if (weekday == 1) {
            return "星期一";
        } else if (weekday == 2) {
            return "星期二";
        } else if (weekday == 3) {
            return "星期三";
        } else if (weekday == 4) {
            return "星期四";
        } else if (weekday == 5) {
            return "星期五";
        } else if (weekday == 6) {
            return "星期六";
        }

        return null;
    }

    public static String getPeriodDate(String date, int period) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null;
        try {
            date1 = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(date1);

        int hour = c.get(Calendar.DAY_OF_MONTH) + period;
        c.set(Calendar.DAY_OF_MONTH, hour);

        Date dateAfter = c.getTime();
        String after = sdf.format(dateAfter);
        return after;
    }

    /**
     * 获取阶段日期
     *
     * @param dateType
     * @author Yangtse
     */
// 使用方法 char datetype = '7';
    public static StringBuilder getPeriodDate(char dateType) {
        Calendar c = Calendar.getInstance(); // 当时的日期和时间
        int hour; // 需要更改的小时
        int day; // 需要更改的天数
        switch (dateType) {
            case '0': // 1小时前
                hour = c.get(Calendar.HOUR_OF_DAY) - 1;
                c.set(Calendar.HOUR_OF_DAY, hour);
// System.out.println(df.format(c.getTime()));
                break;
            case '1': // 2小时前
                hour = c.get(Calendar.HOUR_OF_DAY) - 2;
                c.set(Calendar.HOUR_OF_DAY, hour);
// System.out.println(df.format(c.getTime()));
                break;
            case '2': // 3小时前
                hour = c.get(Calendar.HOUR_OF_DAY) - 3;
                c.set(Calendar.HOUR_OF_DAY, hour);
// System.out.println(df.format(c.getTime()));
                break;
            case '3': // 6小时前
                hour = c.get(Calendar.HOUR_OF_DAY) - 6;
                c.set(Calendar.HOUR_OF_DAY, hour);
// System.out.println(df.format(c.getTime()));
                break;
            case '4': // 12小时前
                hour = c.get(Calendar.HOUR_OF_DAY) - 12;
                c.set(Calendar.HOUR_OF_DAY, hour);
// System.out.println(df.format(c.getTime()));
                break;
            case '5': // 一天前
                day = c.get(Calendar.DAY_OF_MONTH) - 1;
                c.set(Calendar.DAY_OF_MONTH, day);
// System.out.println(df.format(c.getTime()));
                break;
            case '6': // 一星期前
                day = c.get(Calendar.DAY_OF_MONTH) - 7;
                c.set(Calendar.DAY_OF_MONTH, day);
// System.out.println(df.format(c.getTime()));
                break;
            case '7': // 一个月前
                day = c.get(Calendar.DAY_OF_MONTH) - 30;
                c.set(Calendar.DAY_OF_MONTH, day);
// System.out.println(df.format(c.getTime()));
            case '8': // 明天
                day = c.get(Calendar.DAY_OF_MONTH) + 1;
                c.set(Calendar.DAY_OF_MONTH, day);
                break;
            case '9': // 后天
                day = c.get(Calendar.DAY_OF_MONTH) + 2;
                c.set(Calendar.DAY_OF_MONTH, day);
                break;
        }
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);
        StringBuilder strForwardDate = new StringBuilder()
//                .append(mYear)
                .append(((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)) + "月")
                .append(((mDay < 10) ? "0" + mDay : mDay) + "日");
        System.out.println("strDate------->" + strForwardDate + "-" + c.getTimeInMillis());
        return strForwardDate;
    }


    private static List<Calendar> holidayList = new ArrayList<Calendar>();  //节假日列表

    public static Calendar addDateByWorkDay(Calendar calendar, int day) {

        try {
            for (int i = 0; i < day; i++) {

                calendar.add(Calendar.DAY_OF_MONTH, 1);

                if (checkHoliday(calendar)) {
                    i--;
                }
            }
            return calendar;


        } catch (Exception e) {
            e.printStackTrace();
        }
        return calendar;
    }

    public static boolean checkHoliday(Calendar calendar) throws Exception {

        // 判断日期是否是周六周日
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ||
                calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            return true;
        }
        // 判断日期是否是节假日
        for (Calendar ca : holidayList) {
            if (ca.get(Calendar.MONTH) == calendar.get(Calendar.MONTH) &&
                    ca.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH) &&
                    ca.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {
                return true;
            }
        }

        return false;
    }

    private static List<String> getHolidayArray() {
        List<String> holidayArray = new ArrayList<>();
        String string = UtilPreferences.getString(MyApplication.getInstance(), "jiari_string");
        Log.e("TAG", "getHolidayArray: " + string);
        if (string != null) {
            String[] holidays = string.split(",");
            if (holidays != null && holidays.length > 0) {
                for (int i = 0, len = holidays.length; i < len; i++) {
                    if (holidays[i].length() == 4) {
                        String holiday = holidays[i];
                        String m = holiday.substring(0, 2);
                        String d = holiday.substring(2, 4);
                        holiday = m + "-" + d;
                        holidayArray.add(holiday);
                    }
                }
            }
        }
        return holidayArray;
    }

    private static List<String> getNextHolidayArray() {
        List<String> holidayArray = new ArrayList<>();
        String string = UtilPreferences.getString(MyApplication.getInstance(), "next_jiari_string");
        Log.e("TAG", "getNextHolidayArray: " + string);
        if (string != null) {
            String[] holidays = string.split(",");
            if (holidays != null && holidays.length > 0) {
                for (int i = 0, len = holidays.length; i < len; i++) {
                    if (holidays[i].length() == 4) {
                        String holiday = holidays[i];
                        String m = holiday.substring(0, 2);
                        String d = holiday.substring(2, 4);
                        holiday = m + "-" + d;
                        holidayArray.add(holiday);
                    }
                }
            }
        }
        return holidayArray;
    }

    public static void initHolidayList() {

        List<String> holidayArray = getHolidayArray();

        for (String date : holidayArray) {
            String[] da = date.split("-");

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MONTH, Integer.valueOf(da[0]) - 1); // 月份比正常小1,0代表一月
            calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(da[1]));
            holidayList.add(calendar);
        }

    }

    public static void initNextHolidayList() {

        Date date1 = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(date1);
        int nextYear = Integer.valueOf(year) + 1;

        List<String> nextHolidayArray = getNextHolidayArray();

        for (String date : nextHolidayArray) {
            String[] da = date.split("-");

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, nextYear);
            calendar.set(Calendar.MONTH, Integer.valueOf(da[0]) - 1); // 月份比正常小1,0代表一月
            calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(da[1]));
            holidayList.add(calendar);
        }
    }

    // 获取今天的日期
    public static String today() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
        String strDate = formatter.format(curDate);
        return strDate;
    }

    // 获取当前年份
    public static String thisYear() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
        String strDate = formatter.format(curDate);
        return strDate;
    }

    // 获取当前月份
    public static String thisMonth() {
        SimpleDateFormat formatter = new SimpleDateFormat("MM月");
        Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
        String strDate = formatter.format(curDate);
        return strDate;
    }

    // 获取当前月份
    public static int getCurMonth() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        return currentMonth;
    }

    // 获取当前季度
    public static String getCurQuarterIn() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        String now = "";
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                return "第一季度";
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                return "第二季度";
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                return "第三季度";
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                return "第四季度";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    // 获取当前半年
    public static String getCurHalfYearIn() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        String now = "";
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                return "上半年";
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                return "下半年";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;

    }




    public static String ISOtoCommon4(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            Date date = formatter.parse(time);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.HOUR, 8);
            date = cal.getTime();
            return dateToString(date, "yyyy-MM-dd HH:mm");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String ISOtoCommon5(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            Date date = formatter.parse(time);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.HOUR, 8);
            date = cal.getTime();
            return dateToString(date, "MM-dd HH:mm");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String ISOtoCommon6(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            Date date = formatter.parse(time);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.HOUR, 8);
            date = cal.getTime();
            return dateToString(date, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String ISOtoCommon7(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            Date date = formatter.parse(time);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.HOUR, 8);
            date = cal.getTime();
            return dateToString(date, "yyyy年MM月dd日 HH时");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //daterange_todayWithMinute   --  yyyy-MM-dd 00:00:00
    //daterange_curMinute   --   yyyy-MM-dd HH:mm:ss
    //todayWithYear  --  yyyy
    //todayWithMonth  --   yyyy-MM
    //todayWithDay   --  yyyy-MM-dd
    //todayWithMinute -- yyyy-MM-dd
    //curMinute --  yyyy-MM-dd


//    public static String nowTime(String format) {
//        SimpleDateFormat formatter = new SimpleDateFormat(format);
//        Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
//        String strDate = formatter.format(curDate);
//        return strDate;
//    }

    public static String firstdayOfWeek(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        // 获取本周的第一天
        int firstDayOfWeek = calendar.getFirstDayOfWeek() + 1;
        calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
        Date date = calendar.getTime();
        String strDate = formatter.format(date);
        return strDate;
    }


    public static String yesterday(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Calendar c = Calendar.getInstance(); // 当时的日期和时间
        int day; // 需要更改的天数
        day = c.get(Calendar.DAY_OF_MONTH) - 1;
        c.set(Calendar.DAY_OF_MONTH, day);
        Date date = c.getTime();
        String strDate = formatter.format(date);
        return strDate;
    }


    public static String firstdayOfMonth() {
        SimpleDateFormat formatter = new SimpleDateFormat(
                "yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date date = cal.getTime();
        String strDate = formatter.format(date);
        return strDate;
    }



    //region Gate


    public static String daterange_firstdayOfMonth() {
        SimpleDateFormat formatter = new SimpleDateFormat(
                "yyyy-MM-dd 00:00:00");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date date = cal.getTime();
        String strDate = formatter.format(date);
        return strDate;
    }
    //endregion

    /**
     * 调此方法输入所要转换的时间输入例如（"2014-06-14 16:09:00"）返回时间戳
     *
     * @param time
     * @return
     */
    public static String dataOne(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdr.parse(time);
            long l = date.getTime();
            String stf = String.valueOf(l);
            times = stf.substring(0, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }




    //region 台账

    public static String getLastMonthTop(String format) {
        String tm="";
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
            Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
//            Date date = formatter.parse(tm);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(curDate);
            int month = calendar.get(Calendar.MONTH);
            int year = calendar.get(Calendar.YEAR);
            if (month == 0) {
                year = year - 1;
                month = 11;
            } else {
                month = month - 1;
            }

            calendar.set(year, month, 1);

            Date now = calendar.getTime();
            tm = formatter.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return tm;
    }

    public static String getLastMonthEnd(String format) {
        String tm="";
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
//            Date date = formatter.parse(tm);
            Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(curDate);
            int month = calendar.get(Calendar.MONTH);
            int year = calendar.get(Calendar.YEAR);
            int day = getMonthDays(year, month + 1);


            calendar.set(year, month, day);

            Date now = calendar.getTime();
            tm = formatter.format(now);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return tm;

    }

    public static String getLastYearTop(String tm) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = formatter.parse(tm);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int year = calendar.get(Calendar.YEAR) - 1;


            calendar.set(year, 0, 1);

            Date now = calendar.getTime();
            tm = formatter.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return tm;
    }

    public static String getLastYearEnd(String tm) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = formatter.parse(tm);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int year = calendar.get(Calendar.YEAR);
            calendar.set(year, 11, 31);

            Date now = calendar.getTime();
            tm = formatter.format(now);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return tm;

    }

    public static int getYearDays(int year) {
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
            return 366;
        } else {
            return 365;
        }
    }

    public static int getMonthDays(int year, int month) {

//        month++;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                    return 29;
                } else {
                    return 28;
                }
            default:
                return -1;
        }
    }
    //endregion


    public static int timeCompare(String startTime, String endTime,String format) {
        int i = 0;
        //注意：传过来的时间格式必须要和这里填入的时间格式相同
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间
            // 1 结束时间小于开始时间 2 开始时间与结束时间相同 3 结束时间大于开始时间
            if (date2.getTime() < date1.getTime()) {
                //结束时间小于开始时间
                i = 1;
            } else if (date2.getTime() == date1.getTime()) {
                //开始时间与结束时间相同
                i = 2;
            } else if (date2.getTime() > date1.getTime()) {
                //结束时间大于开始时间
                i = 3;
            }
        } catch (Exception e) {

        }
        return i;
    }

    public static String getGapTime(long time){
        long hours = time / (1000 * 60 * 60);
        long minutes = (time-hours*(1000 * 60 * 60 ))/(1000* 60);
        long second = (time-hours*(1000 * 60 * 60 )-minutes*(1000 * 60 ))/1000;
        String diffTime="";
        if(minutes<10){
            diffTime=hours+":0"+minutes;
        }else{
            diffTime=hours+":"+minutes;
        }
        if(second<10){
            diffTime=diffTime+":0"+second;
        }else{
            diffTime=diffTime+":"+second;
        }
        return diffTime;
    }
}
